package cn.itcast.demo.service;

import cn.itcast.demo.mapper.UserMapper;
import cn.itcast.demo.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class Uservice {
    //获取一个日志的工具
    Logger logger = LoggerFactory.getLogger(Uservice.class);


    @Autowired
    private UserMapper userMapper;

    // 查询时候随机睡眠，1-2000毫秒
    public User queryUserById(Long id) {
        long start = System.currentTimeMillis();
        User user = userMapper.selectByPrimaryKey(id);

        try {
            Thread.sleep((int)(Math.random() * 2000 + 1));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();

        logger.info("本次查询的id为：{}，耗时为： {}毫秒", id, end - start);

        return user;
    }
}
